/**
 * 一个组件，需要绑定在task的node上
 */
cc.Class({
    extends: cc.Component,

    properties: {
        /**
         * success:任务完成的Node
         */
        success: cc.Node,
         /**
         * taskname:任务文案的Label
         */
        taskname: cc.Label,
        /**
         * task_level_now:用来领取奖励的时候，记录当前完成的是哪一关
         */
        task_level_now: 0,
         /**
         * Earn_num:金币特效的prefab
         */
        Earn_num: cc.Prefab,

    },
    onLoad() {
        this.Checktask();
        this.schedule(this.Checktask, 1);
        this.success.on("click", this.GetReword, this);
    },
    Checktask() {
        let next_task = Number(Storage.Get_storage(Storage.GameMessageType.task)) + 1;
        //先init当前task
        this.task_level_now = next_task;
        this.success.active = false;
        if (cc.sys.language != cc.sys.LANGUAGE_CHINESE) {
            this.taskname.string = TaskDetail[next_task].task_name_en;
        } else {
            this.taskname.string = TaskDetail[next_task].task_name;   
        }
        // this.taskreword.string = TaskDetail[next_task].task_reword;
        //this.node.runAction(cc.fadeIn(0.5));
        //check任务是否完成
        switch (TaskDetail[next_task].type) {
            case 0:
                this.CheckType0(next_task);
                break;
            case 1:
                this.CheckType1(next_task);
                break;
            default:
                break;
        }
    },
    CheckType0(next_task) {
        //每个游戏单独的方法
        //检查汽车最大等级是否超过目标等级
        if (Global.MaxCarLevel >= TaskDetail[next_task].task_target) {
            //目标完成，发放奖励
            this.showSuccess();

        }
    },
    CheckType1(next_task) {
        //检查当前汽车的产量
        if (Global.CurrCarsEarn >=TaskDetail[next_task].task_target) {
            //目标完成，发放奖励
            this.showSuccess();
        }
    },
    GetReword() {
        MyAudio.PlayEffect_task();
        this.success.active = false;
        // this.node.runAction(cc.hide());
        this.Checktask();
        let reword = TaskDetail[this.task_level_now].task_reword;
        Storage.Set_storage(Storage.GameMessageType.coin, Number(Storage.Get_storage(Storage.GameMessageType.coin)) + Number(reword));
        Storage.Set_storage(Storage.GameMessageType.task, this.task_level_now);
        this.node.getComponent('collect_coin_anim').onPlayCoinAni(this.node);
        this.showCoinAdd(reword);
    },
    showSuccess() {
        
        this.success.active = true;
        let action = cc.repeatForever(cc.sequence(cc.scaleTo(0.5, 1.2), cc.scaleTo(0.5, 0.8)));
        this.success.runAction(action);
    },
    showCoinAdd(num) {
        let earnnum = cc.instantiate(this.Earn_num);
        earnnum.getComponent(cc.Label).string = num;
        earnnum.parent = this.node;
        // var placeAction = cc.place(-425, 0);
        var actionhide = cc.hide();
        var actionTo = cc.jumpTo(0.5, 0, 150, 50, 1);
        var ActionAll = cc.sequence(actionTo, actionhide)
        earnnum.runAction(ActionAll);
    },
    
});
